let app = new Vue({    
    el: "#app",
    data() {
        return {
			form:{
				username:"",
				password:""
			},
			rules:{
				username:[{required:true,message:'用户名不能为空！'}],
				password:[{required:true,message:'密码不能为空！'}],
			}
        }
    },
	mounted() {
		this.$refs.username.focus()
		if (localStorage.getItem('USERNAME')) {
			this.form.username = localStorage.getItem('USERNAME')
		} else {
			this.form.username = ""
		}
		if (localStorage.getItem('PASSWORD')) {
			this.form.password = localStorage.getItem('PASSWORD')
		} else {
			this.form.password = ""
		}

	},
	methods: {
		checkMobile() {
		    var userAgentInfo = navigator.userAgent;
		    var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
		    var flag = false;
		    for (var v = 0; v < Agents.length; v++) {
		        if (userAgentInfo.indexOf(Agents[v]) > 0) {
		            flag = true;
		            break;
		        }
		    }
		    return flag;
		},
		addByEnterKey(e){
			if (e.keyCode == 13) {
				  this.submit();
				  this.fullscreen();
			}
		},
		async submit() {
            this.$refs.form.validate(async valid => {
                if (valid){
                    try {
                        let res = await axios.post('action/login', this.form);
                        if (res.code === 200) {
							// 设定本地SESSION
							localStorage.setItem('TOKEN',res.data.token)
							localStorage.setItem('USERID',res.data.userid)
							localStorage.setItem('USERNAME',res.data.username)
							localStorage.setItem('PASSWORD',this.form.password)
							localStorage.setItem('ISLEADER',res.data.isleader)
							localStorage.setItem('DEPTID',res.data.deptid)
							
							localStorage.setItem('DEPTNAME',res.data.deptname)
							localStorage.setItem('ISMANAGER',res.data.ismanager)
							if (this.checkMobile()){
							    location.href="../service/p_desktop.html";
							}else {
							   location.href="../desktop";
							}
							// 跳转到主页
							//location.href="../desktop";
                        }else {
                            this.$message.error(res.message);
                        }
                    }catch (e) {
                        this.$message.error('登录请求出错！', e.message);
                    }
                }
            })			
		},
	}
});	